<?php
namespace app\service;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Cache;
use think\Log;
use think\Exception;
use think\Db;

class RedisEvent extends Command
{
    protected function configure()
    {
        $this->setName('RedisEvent')->setDescription('Here is the RedisEventService');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始监听...');
        Cache::store('redis')->handler()->config('notify-keyspace-events','Ex');
        Cache::store('redis')->handler()->setOption(\Redis::OPT_READ_TIMEOUT, -1);
        Cache::store('redis')->handler()->psubscribe(array('__keyevent@0__:expired'), 'app\service\RedisEvent::key_callback');
    }
    public static function key_callback($redis, $pattern, $channel, $payload){
        echo "Payload: $payload\n";
        /*TODO处理业务逻辑*/
        $msginfo = explode(':', $payload);
        if(sizeof($msginfo) == 3){
            $env = $msginfo[0];
            if ($env != getenv('APP_NAME_EN')) {
                return;
            }
            $type = $msginfo[1];
            $value = $msginfo[2];
            if($type == 'lianmai'){
                try {
                    CheckLianmai::CheckLianmai($value);
                } catch (Exception $e) {
                    Log::write(date('YmdHis') . '-连麦扣费错误:order-' . $value . '-msg:' . $e->getMessage());
                }
                // _rabbitMqPublish('lianmai', [
                //     'class' => '\app\service\CheckLianmai',
                //     'method'   => 'CheckLianmai',
                //     'params'    => [$value]
                // ]);
            } else if ($type == 'MYSQL_REFRESH_CONNECT') {
                Db::name('admin')->find();
            } else if ($type == 'refreshstate') {
                _rabbitMqPublish('action', [
                    'action' => 'api/user/setstate',
                    'data'   => ['user_id'=>$value]
                ]);
            } else if ($type == 'rabbitMQReConsumption') {
                _rabbitMqPublish('ffmpeg', [
                    'id' => $value
                ]);
            } else if ($type == 'test') {
                _rabbitMqPublish('class_event', [
                    'class' => '\think\Cache',
                    'method'   => 'set',
                    'params'    => ['t'.time() . ':'. $value, '1', 120]
                ]);
                // Cache::set('t'.time() . ':'. $value, '1', 120);
            }
        } else if (sizeof($msginfo) == 4) {
            $env = $msginfo[0];
            if ($env != getenv('APP_NAME_EN')) {
                return;
            }
            $type = $msginfo[1];
            $value = $msginfo[2];
            $value2 = $msginfo[3];
            if ($type == 'lianmaiuserdq') {
                Log::write('用户'.$value.'断签 - ' . datetime());
                _rabbitMqPublish('action', [
                    'action' => 'api/test/lianmaiStatus',
                    'data'   => ['value' => $value, 'value2' => $value2, 'state' => 0, 'time' => time()],
                ]);
            } else if ($type == 'lianmaiuserxq') {
                Log::write('用户'.$value.'续签 - ' . datetime());
                _rabbitMqPublish('action', [
                    'action' => 'api/test/lianmaiStatus',
                    'data'   => ['value' => $value, 'value2' => $value2, 'state' => 1, 'time' => time()-3],
                ]);
            }
        }
    }

}
